/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package preparer;

import java.util.ArrayList;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.tiles.AttributeContext;
import org.apache.tiles.context.TilesRequestContext;
import org.apache.tiles.preparer.ViewPreparer;
import pojo.SanPham;
import pojo.ThamSo;
import service.ChiTietDonDatHangService;
import service.NhomSanPhamService;
import service.SanPhamService;
import service.ThamSoService;
import service.TheLoaiService;

/**
 *
 * @author pvhuy
 */
public class LeftPreparer implements ViewPreparer {

    private NhomSanPhamService nhomSanPhamService;
    private TheLoaiService theLoaiService;
    private SanPhamService sanPhamService;
    private ChiTietDonDatHangService chiTietDonDatHangService;
    private ThamSoService thamSoService;

    @Override
    public void execute(TilesRequestContext trc, AttributeContext ac) {
        //Right
        // <editor-fold defaultstate="collapsed" desc="1. Truy cap  nhanh gio hang">
        HttpServletRequest request = (HttpServletRequest) trc.getRequest();
        HttpSession session = request.getSession();
        ArrayList<SanPham> gioHang = null;
        if (session.getAttribute("GioHang") == null) {
            gioHang = new ArrayList<SanPham>();
            session.setAttribute("GioHang", gioHang);
        } else {
            gioHang = (ArrayList<SanPham>) session.getAttribute("GioHang");
        }

        double tongGiaTri = 0;
        for (int i = 0; i < gioHang.size(); i++) {
            SanPham sp = gioHang.get(i);
            tongGiaTri = tongGiaTri + sp.getSoLuongDangCo() * sp.getGiaBanHienTai();
        }
        trc.getRequestScope().put("soLuongSpTrongGios", gioHang.size());
        trc.getRequestScope().put("tongGiaTienSPTrongGios", tongGiaTri);
        // </editor-fold>

        ThamSo thamSo = thamSoService.getThamSoByMaThamSo(3);
        trc.getRequestScope().put("listSanPhams", sanPhamService.listSanPhamMoiNhapVe(thamSo.getSoLuongSanPham(), "Moi"));
        trc.getRequestScope().put("listSanPhamBanChayNhats", chiTietDonDatHangService.listSanPhamBanChayNhat(thamSo.getSoLuongSanPham()));
        trc.getRequestScope().put("listSapPhatHanhs", sanPhamService.listSanPhamMoiNhapVe(thamSo.getSoLuongSanPham(), "SapPhatHanh"));

        //Left
        trc.getRequestScope().put("nhomSanPhams", nhomSanPhamService.listNhomSanPham());
        // HttpServletRequest request = (HttpServletRequest) trc.getRequest();
        String str = request.getParameter("NhomSanPhamID");
        if (str != null) {
            int iNhomSanPham = Integer.parseInt(str);
            trc.getRequestScope().put("theLoais", theLoaiService.listTheLoaiByMaNhom(iNhomSanPham));
            trc.getRequestScope().put("tenNhomSanPham", nhomSanPhamService.getTenNhomById(iNhomSanPham));
        } else {
            trc.getRequestScope().put("theLoais", theLoaiService.listTheLoaiByMaNhom(1));
            trc.getRequestScope().put("tenNhomSanPham", nhomSanPhamService.getTenNhomById(1));
        }
    }

    /**
     * @return the nhomSanPhamService
     */
    public NhomSanPhamService getNhomSanPhamService() {
        return nhomSanPhamService;
    }

    /**
     * @param nhomSanPhamService the nhomSanPhamService to set
     */
    public void setNhomSanPhamService(NhomSanPhamService nhomSanPhamService) {
        this.nhomSanPhamService = nhomSanPhamService;
    }

    /**
     * @return the theLoaiService
     */
    public TheLoaiService getTheLoaiService() {
        return theLoaiService;
    }

    /**
     * @param theLoaiService the theLoaiService to set
     */
    public void setTheLoaiService(TheLoaiService theLoaiService) {
        this.theLoaiService = theLoaiService;
    }

    public SanPhamService getSanPhamService() {
        return sanPhamService;
    }

    public void setSanPhamService(SanPhamService sanPhamService) {
        this.sanPhamService = sanPhamService;
    }

    public ChiTietDonDatHangService getChiTietDonDatHangService() {
        return chiTietDonDatHangService;
    }

    public void setChiTietDonDatHangService(ChiTietDonDatHangService chiTietDonDatHangService) {
        this.chiTietDonDatHangService = chiTietDonDatHangService;
    }

    public ThamSoService getThamSoService() {
        return thamSoService;
    }

    public void setThamSoService(ThamSoService thamSoService) {
        this.thamSoService = thamSoService;
    }
}
